package com.xls.box2023.jinbi.tixian;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.lzz.bottomencapsulation.consts.EventbusCode;
import com.lzz.bottomencapsulation.eventbus.MessageEvent;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.lzz.bottomencapsulation.utils.AppUtils;
import com.lzz.bottomencapsulation.utils.TUtils;
import com.xls.box2023.Consts.IntentConsts;
import com.xls.box2023.jinbi.JinBiNetWorkUtil;
import com.xls.box2023.jinbi.tixianjilu.TiXianJiLuActivity;
import com.xls.box2023.login.LoginNetworkUtil;
import com.xls.box2023.login.LoginUtil;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.box2023.network.CallbackT;
import com.xls.box2023.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

/**
 * MVPPlugin
 */

public class TiXianPresenter extends BasePresenterImpl<TiXianContract.View> implements TiXianContract.Presenter{

    @Override
    public void getPermissions(String numStr) {
        XXPermissions.with(activity)
                // 申请单个权限
                .permission(Permission.READ_PHONE_STATE)
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_PHONE_NUMBERS)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.READ_MEDIA_VIDEO)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            LoginNetworkUtil.getInstance().getLoginParams2(activity, new CallbackT<Map<String, String>>() {
                                @Override
                                public void callback(Map<String, String> stringStringMap) {
                                    stringStringMap.put("appName", AppUtils.getPackageName(activity));
                                    stringStringMap.put("token", SPUtils.getLoginToken());
                                    fkCheck(stringStringMap,numStr);
                                }
                            });
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            XXPermissions.startPermissionActivity(activity, permissions);
                        } else {
                            TUtils.showShort(context,"请授予权限,并重新登陆");
                            LoginUtil.getInstance().outLogin(activity);
                        }
                    }
                });
    }

    @Override
    public void fkCheck(Map<String, String> stringStringMap, String numStr) {


        LoginNetworkUtil.getInstance().fkCheck(activity,"box_jinbitixian",stringStringMap, new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(String requestData) {
                tiXian(numStr);            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

    @Override
    public void tiXian(String numStr) {
//        Double maxnum=getMAxNum();
//        Double num=Double.parseDouble(numStr);
//        if (num>maxnum){
//            TUtils.showShort(context,"输入的金额大于余额,请重新输入");
//            return;
//        }
        JinBiNetWorkUtil.getInstance().coinsExchange(numStr, new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(String requestData) {
                EventBus.getDefault().post(new MessageEvent(EventbusCode.CODE_TIXIAN_OK));
                if (mView==null||context==null)return;
                TUtils.showShort(context,"提现成功");
                TiXianJiLuActivity.startActivity(activity);
                activity.finish();
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {
                TUtils.showShort(context,errmsg);
            }
        });
    }

    private Double getMAxNum() {
        String maxnum=activity.getIntent().getStringExtra(IntentConsts.data);
        return Double.parseDouble(maxnum);
    }
}
